import API from '../../../api/feedback_admin';
import { formatTime, errorFun } from '../../../utils/index';

Page({
    data: {
        radioItems: [
            { name: '显示全部', value: '', checked: 'true' },
            { name: '功能异常', value: '功能异常' },
            { name: '投诉举报', value: '投诉举报' },
            { name: '优化建议', value: '优化建议' }
        ],
        feedbackList: [],
        selectArray: [
            {
                text: '所有小程序',
                value: ''
            }
        ],
        formData: {
            radio: '',
            select: ''
        }
    },
    page_no: 1,
    page_size: 6,
    onLoad() {
        this.getMiniNameList();
        this.getFeedbackList('','',true).then((res) => {
            this.setData({
                feedbackList: res
            });
        });
    },
    onReachBottom() {
        this.page_no = this.page_no + 1;
        let { radio, select } = this.data.formData;
        this.getFeedbackList(select, radio, false).then((res) => {
            this.setData({
                feedbackList: this.data.feedbackList.concat(res)
            });
        });
    },

    radioChange(evt) {
        let { value } = evt.detail;
        this.page_no = 1;
        this.setData({
            feedbackList: [],
            'formData.radio': value
        });
        let { radio, select } = this.data.formData;
        this.getFeedbackList(select, radio).then((res) => {
            this.setData({
                feedbackList: res
            });
        });
    },
    select(evt) {
        let { value } = evt.detail;
        this.page_no = 1;
        this.setData({
            feedbackList: [],
            'formData.select': value
        });
        let { radio, select } = this.data.formData;
        this.getFeedbackList(select, radio).then((res) => {
            this.setData({
                feedbackList: res
            });
        });
    },

    getFeedbackList(text = '', type = '', isFirst = false) {
        if (isFirst) {
            wx.showLoading({
                title: '加载中',
                mask: true
            });
        } else {
            wx.showNavigationBarLoading();
        }
        return new Promise((resolve, reject) => {
            API.getFeedbackList(text, type, this.page_no, this.page_size)
                .then((res) => {
                    if (res) {
                        let newRes = res.map((item) => {
                            return {
                                ...item,
                                imgs: JSON.parse(item.imgs),
                                time: formatTime(Number(item.time)),
                                content: this.uncodeUtf16(item.content)
                            };
                        });
                        resolve(newRes);
                    }
                })
                .catch((err) => {
                    errorFun(err);
                    reject(err);
                }).finally(() => {
                    wx.hideLoading();
                    wx.hideNavigationBarLoading();
                })
        });
    },
    getMiniNameList() {
        return new Promise((resolve, reject) => {
            API.getNameList()
                .then((res) => {
                    let arr = [];
                    res.map((item) => {
                        return arr.push({
                            text: item.name,
                            value: item.name
                        });
                    });
                    this.setData({
                        selectArray: this.data.selectArray.concat(arr)
                    });
                    resolve();
                })
                .catch((err) => {
                    errorFun(err);
                    reject(err);
                });
        });
    },
    previewImg(evt) {
        let { img } = evt.currentTarget.dataset;
        wx.previewImage({
            urls: [img],
            showmenu: true
        });
    },
    copyNo(evt) {
        let { info } = evt.currentTarget.dataset;
        wx.setClipboardData({
            data: info,
            success: () => {
                wx.getClipboardData({
                    success: () => {
                        wx.showToast({
                            title: '已复制',
                            mask: true
                        });
                    }
                });
            }
        });
    },
    // 解码emoji表情
    uncodeUtf16(str) {
        var reg = /\&#.*?;/g;
        var result = str.replace(reg, function (char) {
            var H, L, code;
            if (char.length == 9) {
                code = parseInt(char.match(/[0-9]+/g));
                H = Math.floor((code - 0x10000) / 0x400) + 0xd800;
                L = ((code - 0x10000) % 0x400) + 0xdc00;
                return unescape('%u' + H.toString(16) + '%u' + L.toString(16));
            } else {
                return char;
            }
        });
        return result;
    }
});
